const { DataSystemDeptModel } = require("../models/sys_dept");
const { fail, CODE, success } = require("../utils/utils");
class SystemDeptController {
  /**
   * @description 添加部门
   * @param {String} dept_name
   * @param {Number} parent_id
   * @param {Number} order_num
   * @param {String} leader
   * @param {String} phone
   * @param {String} email
   * @param {String} status
   * @param {String} op_user_id
   */
  static async sysDeptAdd (ctx, next) {
    const {
      dept_name,
      parent_id,
      order_num,
      leader,
      phone,
      email,
      status = "1",
      remark,
    } = ctx.request.body;
    try {
      const { user_id: op_user_id } = ctx.state.user;
      if (!dept_name) {
        return (ctx.body = fail([], `请输入部门`, CODE.BUSINESS_ERROR));
      }
      await DataSystemDeptModel.create({
        dept_name,
        parent_id,
        order_num,
        leader,
        phone,
        email,
        status,
        remark,
        op_user_id,
      });
      return (ctx.body = success([], `新增部门成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
  /**
   * @description 更新部门
   * @param {Number} dept_id
   * @param {String} dept_name
   * @param {Number} parent_id
   * @param {Number} order_num
   * @param {String} leader
   * @param {String} phone
   * @param {String} email
   * @param {String} status
   * @param {String} op_user_id
   */
  static async sysDeptUpd (ctx, next) {
    const {
      dept_id,
      dept_name,
      parent_id,
      order_num,
      leader,
      phone,
      email,
      status = "1",
      remark,
    } = ctx.request.body;
    try {
      const { user_id: op_user_id } = ctx.state.user;
      if (!dept_id) {
        return (ctx.body = fail([], `请输入必填项`, CODE.BUSINESS_ERROR));
      }
      await DataSystemDeptModel.update({
        dept_id,
        dept_name,
        parent_id,
        order_num,
        leader,
        phone,
        email,
        status,
        remark,
        op_user_id,
      });
      return (ctx.body = success([], `部门更新成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
  /**
   * @description 删除部门
   * @param {Number} dept_id
   */
  static async sysDeptDel (ctx, next) {
    const { dept_id } = ctx.request.body;
    try {
      const { user_id: op_user_id } = ctx.state.user;
      if (!dept_id) {
        return (ctx.body = fail([], `dept_id 获取失败`, CODE.BUSINESS_ERROR));
      }
      await DataSystemDeptModel.delete({ op_user_id, dept_id });
      return (ctx.body = success([], `部门删除成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }

  /**
   * @description 批量删除部门
   * @param {Number} ids
   */
  static async sysDeptDelByIds (ctx, next) {
    const { ids } = ctx.request.body;
    try {
      const { user_id: op_user_id } = ctx.state.user;
      if (!ids || !Array.isArray(ids) || ids.length === 0) {
        return (ctx.body = fail([], `删除失败`, CODE.BUSINESS_ERROR));
      }
      await DataSystemDeptModel.deleteByIds({ op_user_id, ids });
      return (ctx.body = success([], `部门批量删除成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
  /**
   * @description 部门所有列表
   * @param {String} name
   * @param {String} status
   */
  static async sysDeptList (ctx, next) {
    try {
      const { dept_name,status } = ctx.request.query;
      const { data_scope = "" } = ctx.state;
      const result = await DataSystemDeptModel.list({ data_scope,dept_name,status });
      return (ctx.body = success({ result }, `获取部门数据成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
  /**
   * @description 部门所有列表
   * @param {String} name
   * @param {String} status
   */
  static async sysAllDeptList (ctx, next) {
    try {
      const { data_scope = "" } = ctx.state;
      const result = await DataSystemDeptModel.all({ data_scope });
      return (ctx.body = success({ result }, `获取部门数据成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
  /**
   * @description 通过部门id获取数据
   * @param {Number} dept_id
   */
  static async sysDeptById (ctx, next) {
    const { dept_id } = ctx.request.query;
    try {
      if (!dept_id) {
        return (ctx.body = fail([], `dept_id 获取失败`, CODE.BUSINESS_ERROR));
      }
      const result = await DataSystemDeptModel.findBySystemDeptId({
        dept_id: Number(dept_id),
      });
      return (ctx.body = success(result, `获取部门数据成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }

  /**
   * @description 通过部门id获取数据
   * @param {Number} dept_id
   */
  static async sysDeptExclude (ctx, next) {
    const { dept_id } = ctx.request.query;
    try {
      if (!dept_id) {
        return (ctx.body = fail([], `dept_id 获取失败`, CODE.BUSINESS_ERROR));
      }
      const { data, error } =
        await DataSystemDeptModel.findExcludeBySystemDeptId({
          dept_id: Number(dept_id),
        });
      if (error) {
        return (ctx.body = fail([], error.message, CODE.fail));
      } else {
        return (ctx.body = success(data, `获取部门数据成功`, CODE.SUCCESS));
      }
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
  /**
   * @description 通过角色id获取数据
   * @param {Number} 
   */
  static async deptTree (ctx, next) {
    try {
      const { data_scope = "" } = ctx.state;
      const deptList = await DataSystemDeptModel.all({ data_scope });
      const dept = (function makeTreeData (arr, parent_id) {
        let temp = []
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].parent_id === parent_id) {
            temp.push(arr[i])
            arr[i].children = makeTreeData(deptList, arr[i].id)
          }
        }
        return temp
      })(deptList, 0)
      return (ctx.body = success({ dept }, `获取菜单数据成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
  /**
   * @description 通过角色id获取数据
   * @param {Number} role_id
   */
  static async roleDeptTreeselect (ctx, next) {
    const { role_id } = ctx.request.body;
    try {
      if (!role_id) {
        return (ctx.body = fail([], `role_id 获取失败`, CODE.BUSINESS_ERROR));
      }
      const { data_scope = "" } = ctx.state;
      const deptByRoleIds = await DataSystemDeptModel.findBySystemRoleId({
        role_id: Number(role_id),
      });
      const checkedKeys = deptByRoleIds.map(item => item.dept_id);
      const deptsList = await DataSystemDeptModel.all({ data_scope });
      const depts = (function makeTreeData (arr, parent_id) {
        let temp = []
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].parent_id === parent_id) {
            temp.push(arr[i])
            arr[i].children = makeTreeData(deptsList, arr[i].id)
          }
        }
        return temp
      })(deptsList, 0)
      return (ctx.body = success({ depts, checkedKeys }, `获取菜单数据成功`, CODE.SUCCESS));
    } catch (error) {
      return (ctx.body = fail([], error.message, CODE.BUSINESS_ERROR));
    }
  }
}

module.exports = {
  SystemDeptController,
};
